package day20180128;

/**
 * Created by 14394 on 2018/1/28.
 */
public class TestThrowable {

    //测试的方法的最高层,抛出异常到上一层
    static void a() throws HighLevelException {
        try {
            b();
            System.out.println("执行顺序：5");
            //捕捉的异常，注意只有上文抛出的具体对象才能捕捉
            //注意可以捕捉超类，具体子类只能捕捉上文抛出过的对象，如midLevelException
        }catch (MidLevelException e){
            //捕捉到该异常的处理，即是手动抛出新的异常
            throw new HighLevelException(e);
        }
    }

    //如果上文有抛出异常，必须进行捕捉
     static void b() throws MidLevelException{
        c();
         System.out.println("执行顺序：4");
    }

    static void c() throws MidLevelException{
        try {
            d();
            System.out.println("执行顺序：3");

        }catch (LowLevelException e){
            throw new MidLevelException(e);
        }
    }

    private static void d() throws LowLevelException{
        e();
        System.out.println("执行顺序：2");
    }

    private static void e() throws LowLevelException {
        //手动抛出异常，给上层  即e()方法
        System.out.println("执行顺序：1");
        throw new LowLevelException();
    }

    public static void main(String[] args) {
        try {
            a();
        } catch (HighLevelException e) {
            e.printStackTrace();
        }
    }


}

class HighLevelException extends Exception {
    HighLevelException(Throwable cause) { super(cause); }
}

class MidLevelException extends Exception {
    MidLevelException(Throwable cause)  { super(cause); }
}

class LowLevelException extends Exception {
}
